package com.hnevc.gui;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class KeyEventDemo extends JFrame {
	JPanel p;
	BufferedImage image ;
	JButton button;
	public KeyEventDemo(){
		p = new JPanel();
		p.setBackground(Color.black);
		button = new JButton("按钮");
		button.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				int x = button.getX();
				int y = button.getY();
				switch(key){
					case KeyEvent.VK_RIGHT:
						button.setLocation(x + 5, y);
						break;
					case KeyEvent.VK_LEFT:
						button.setLocation(x - 5, y);
						break;
					case KeyEvent.VK_UP:
						button.setLocation(x, y - 5);
						break;
					case KeyEvent.VK_DOWN:
						button.setLocation(x, y + 5);
						break;
				}
				
			}
		});
		p.add(button);
		this.add(p);
		this.setLocation(200, 100);
		this.setSize(300, 500);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new KeyEventDemo();
	}

}
